const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainees: []
  },
  addTrainee: function() {
    wx.navigateTo({
      url: '/pages/addTrainee/addTrainee',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.refleshData();
  },
  refleshData: function() {
    const that = this;
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    if (openId.length==0){
      return;
    }
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/coach/traineeList/' + openId,
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const traineeList = response.data.content;
          traineeList.forEach(function(member){
            if(member.sex=='MAN'){
              member.sex='先生';
            }else{
              member.sex = '小姐';
            }
          });
          that.setData({
            trainees: traineeList
          });
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refleshData();
  },

  toTrain:function(e){
    const index = e.currentTarget.dataset.index;
    const trainees = this.data.trainees;
    const selectTrainee = trainees[index];
    wx.setStorageSync("shipId", selectTrainee.id);
    wx.setStorageSync("traineePhone", selectTrainee.traineePhone);
    wx.navigateTo({
      url: '/pages/trainModule/trainModule',
    })
  },

  toDeleteRelationship: function(e) {
    const that = this;
    const url = app.globalData.url;
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除此会员？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: url + '/coach/deleteTrainee/' + id,
            success: function (response) {
              if (response.data.success) {
                that.refleshData();
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'none'
                });
              }
            }
          })
        }
      }
    })
  }
})